﻿//-----------------------------------------------------------------------
// <copyright file="DestroyGroupAction.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Action.Group
{
    using System;
    using System.Collections.Generic;
    using TileTool.Log;
    using TileTool.Scene.Entity;
    using TileTool.Scene.Entity.Group;

    /// <summary>
    /// Action to destroy a group.
    /// </summary>
    public class DestroyGroupAction : SceneAction
    {
        #region Constants - Private

        /// <summary>
        /// Log message group.
        /// </summary>
        private const string LogGroup = "GestroyGroupAction";

        #endregion

        #region Fields - Private

        /// <summary>
        /// Group entities we are going to destroy.
        /// </summary>
        private IEntityCollection destroyGroups;

        /// <summary>
        /// Entities that we children of a group.
        /// </summary>
        private IEntityCollection groupChildren;

        /// <summary>
        /// Dictionary which maps from an entity to its group parent.
        /// </summary>
        private IDictionary<IEntity, IEntity> entityToGroupDict;

        /// <summary>
        /// Scene selection before group was added.
        /// </summary>
        private IEntityCollection oldSelection;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for the action.
        /// </summary>
        /// <param name="scene">Scene we are creating a group in.</param>
        /// <param name="ents">Entities to create a group with.</param>
        public DestroyGroupAction(ISceneManager scene, IEntityCollection ents)
            : base(scene)
        {
            if (scene == null)
            {
                throw new ArgumentNullException("scene", "Scene must be non-null.");
            }

            if (ents == null)
            {
                throw new ArgumentNullException("ents", "Entity collection must be non-null..");
            }

            if (ents.Count == 0)
            {
                throw new ArgumentException("Entity collection count must be non-zero.");
            }

            this.oldSelection = this.Scene.Selection;
            
            this.BuildDestroyGroups(ents);
            this.BuildGroupChildren();
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Do the action.
        /// </summary>
        public override void Do()
        {
            LogSystem.LogInfo(LogGroup, "Do() - Destroying {0} groups.", this.destroyGroups.Count);

            foreach (IEntity entity in this.groupChildren)
            {
                entity.Parent = null;
            }

            Scene.RemoveEntities(this.destroyGroups);
            Scene.SetSelection(this.groupChildren);
        }

        /// <summary>
        /// Undo the action.
        /// </summary>
        public override void Undo()
        {
            LogSystem.LogInfo(LogGroup, "Undo() - Recreating {0} groups.", this.destroyGroups.Count);

            foreach (IEntity ent in this.groupChildren)
            {
                ent.Parent = this.entityToGroupDict[ent];
            }

            Scene.AddEntities(this.destroyGroups);
            Scene.SetSelection(this.oldSelection);
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Given a source set of entities, build a list of the entities
        /// that are groups in the input list.
        /// </summary>
        /// <param name="src">Entity collection to build group list from.</param>
        private void BuildDestroyGroups(IEntityCollection src)
        {
            this.destroyGroups = new EntityCollection();

            foreach (IEntity entity in src)
            {
                if (entity as GroupEntity != null)
                {
                    this.destroyGroups.Add(entity);
                }
            }
        }

        /// <summary>
        /// Build the group children list and dictionary.
        /// </summary>
        private void BuildGroupChildren()
        {
            this.groupChildren = new EntityCollection();
            this.entityToGroupDict = new Dictionary<IEntity, IEntity>();

            foreach (IEntity group in this.destroyGroups)
            {
                IEntityCollection children = group.Children;
                foreach (IEntity child in children)
                {
                    this.groupChildren.Add(child);
                    this.entityToGroupDict.Add(child, group);
                }
            }
        }

        #endregion
    }
}
